﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.IO;


namespace RuneLogLoader
{
    class DatabaseWriter
    {
        public string FileName
        {
            get;
            private set;
        }
        private SQLiteConnection sql;

        private string[] DDLStatements = { "CREATE TABLE Items (Name varchar(30), id int UNIQUE)", 
                                           "CREATE TABLE Settings (Key varchar(30), Value varchar(30))" };

        public DatabaseWriter()
        {
            FileName = DateTime.Now.ToString("MM-dd-yy hh-mm-ss") + ".sqlite";
            File.Create(FileName).Close();
            sql = new SQLiteConnection("Data Source=" + FileName + ";Version=3;");
            sql.Open();
            RunDDL();
        }

        private void RunDDL()
        {
            foreach (string s in DDLStatements)
            {
                new SQLiteCommand(s, sql).ExecuteNonQuery();
            }
        }

        public void WriteToDatabase(string name, int id)
        {
            new SQLiteCommand(String.Format("INSERT INTO Items (Name, id) VALUES (\"{0}\", {1})", name.Replace("'", "#$"), id), sql).ExecuteNonQuery();
        }
        
        public void WriteToDatabase(Dictionary<int, string> list) {
            foreach (var kvp in list)
            {
                WriteToDatabase(kvp.Value, kvp.Key);
            }
        }
    }
}
